library(testthat)
context("avm test")

test_that("avm test", {
    con<-dbconnect()
    tabln.vec<-loadData(con=con,month_offset = -2)
    poi.data.list<-getpoi.ras(con=con,tabln.vec = tabln.vec)
    tabln.vec$ha_info.sp<-poi.data.list$ha_info.sp
    killDbConnections()
    var.names.except<-c('city_code','ha_code','proptype','year','month',
                        'salecount','rentcount','name','ha_cl_code',
                        'ha_cl_name','dist_code',
                        'dist_name')
    ppi_sale<-ppsale(tabln.vec = tabln.vec)
    sale<-ppi_sale%>%dplyr::select(-one_of(var.names.except))

    sale$yearmonth<-paste(ppi_sale$year,ppi_sale$month,sep = '-')
    date<-sort(unique(sale$yearmonth))
    len<-length(date)
    fit.vec_city<-vector(mode='list',len)
    names(fit.vec_city)<-date
    formula.lm.saleprice<-
        'log(saleprice)~.-yearmonth-ymid-volume_rate-greening_rate-x_-y_-rentprice-rentbldgarea-salebldgarea-bldg_type'%>%
        as.formula
    library(formula.tools)
    rvars<-rhs.vars(formula.lm.saleprice,data=sale)
    formula.lm.saleprice.2<-paste(lhs.vars(formula.lm.saleprice)%>%attr('term.labels'),
                        paste(rvars,collapse = '+'),sep='~')%>% as.formula
    # for (i in 1:len){
    #     origin.y<-
    #         lhs.vars(formula.lm.saleprice)%>%as.character()%>%str_replace_all('~log\\(|\\)','')
    #     sale.new<-sale%>%filter(yearmonth==date[i])
    #     sale.new<-sale.new[sale.new[,origin.y]>0,]
    #     fit.vec_city[[i]]<-step(lm(formula.lm.saleprice.2, data = sale.new),trace=F)
    # }
    # sapply(fit.vec_city,function(x) summary(x)$adj.r.square)%>%fivenum
    dependY.raw<-
        lhs.vars(formula.lm.saleprice)%>%as.character()%>%str_replace_all('~log\\(|\\)','')
    sale.new<-sale%>%filter(yearmonth==date[1])
    idx<-sale.new[,dependY.raw]>0
    data.reg<-sale.new[idx,]
    # Linear Regression
    avm.reg.ols<-step(lm(formula.lm.saleprice.2,data=data.reg),trace=F)
    # Robust Linear Regression
    library(MASS)
    avm.reg.rlm <- rlm(formula.lm.saleprice.2,data=data.reg)
    # tune.lm(lm, formula.lm.saleprice.2, data = data.reg)
    # summary(avm.reg.ols)
    # Support Vector Machine
    library(e1071)
    avm.reg.svm<-svm(formula.lm.saleprice.2,data=data.reg)
    # Decision Tree
    # avm.reg.rpart<-best.rpart(formula.lm.saleprice.2,data=data.reg,minsplit = c(5,10,15))
    # Random Forests
    library(randomForest)
    avm.reg.rft <- randomForest(formula.lm.saleprice.2,data=data.reg, ntree=500)
    # Gradient Boosting Machine
    # library(gbm)
    # avm.reg.gbm <- gbm(formula.lm.saleprice.2,data=data.reg,n.trees=500,shrinkage=0.005 ,cv.folds=10)
    # best.iter <- gbm.perf(avm.reg.gbm,method="cv")
    # train_pred <- predict.gbm(GBM_model,trainset,best.iter)
    # test_pred <- predict.gbm(GBM_model,testset,best.iter)
    # library(earth)
    # avm.reg.ear <- earth(formula.lm.saleprice.2,data=data.reg)
    library(maptools)
    rvars.withoutxy<-rvars[rvars!='x'&rvars!='y'&rvars!='bldg_code']
    formula.gwr.saleprice<-paste(lhs.vars(formula.lm.saleprice)%>%attr('term.labels'),
                                 paste(rvars.withoutxy,collapse = '+'),sep='~')%>% as.formula
    data.reg.sp<-sp.pts(data_ = data.reg%>%dplyr::filter(buildyear>=1900)%>%na.omit())
    data.reg.sp$bldg_code<-as.factor(data.reg.sp$bldg_code)

    library(GWmodel)
    DM<-gw.dist(dp.locat=coordinates(data.reg.sp),longlat = T)
    bw1<-bw.gwr(formula.gwr.saleprice, data=data.reg.sp,dMat=DM,longlat = T,approach = 'AIC')
    avm.reg.gwr<-gwr.basic(formula.gwr.saleprice, data=data.reg.sp, bw=bw1,longlat = T,dMat=DM)
    res1<-gwr.basic(formula.gwr.saleprice, data=data.reg.sp, bw=bw1,longlat = T,
                    regression.points = data.reg.sp[1,])
    library(spgwr)
    bw.gwr.vl<-gwr.sel(formula = formula.gwr.saleprice,data=data.reg.sp,longlat = T,gweight =gwr.bisquare )

    i<-200
    gwr.pred<-gwr.predict(formula.gwr.saleprice, data=data.reg.sp, bw=bw1,
                          predictdata = data.reg.sp[i,],longlat = T)

    avm.reg.gwr.2<-gwr(formula = formula.gwr.saleprice, data = data.reg.sp[1:100,],
                       gweight = gwr.bisquare,hatmatrix =F,bandwidth = bw1,
                       fit.points =data.reg.sp[i,],predictions = T)
    data.reg$saleprice[i]
    predict(avm.reg.ols)[i]%>%exp()
    predict(avm.reg.gbm)[i]%>%exp()
    predict(avm.reg.rlm)[i]%>%exp()
    predict(avm.reg.rft)[i]%>%exp()
    # predict(avm.reg.rpart)[i]%>%exp()
    predict(avm.reg.svm)[i]%>%exp()
    avm.reg.gwr$SDF$yhat[i]%>%exp()
    gwr.pred$SDF$prediction%>%exp()

    avm.reg.gwr.2$SDF$pred%>%exp()

    # for (i in 1:len){
    #     sale.new<-sale%>%filter(ymid==date[i])%>%na.omit
    #     coordinates(sale.new)<-~x+y
    #     proj4string(sale.new)<-'+init=epsg:4326'
    #     bw.gwr.vl<-gwr.sel(formula =formula.gwr,data=sale.new,longlat = T)
    #     fit.vec_city.gwr[[i]]<-gwr(formula.gwr, data = sale.new,
    #                                hatmatrix = T,
    #                                bandwidth = bw.gwr.vl)
    # }

})
